package login;
import java.awt.Dimension; 
import java.awt.Image;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;


 public class Registrazione extends JFrame implements ActionListener{
	  ImagePanell panel = new ImagePanell(new ImageIcon("C:\\Users\\acer\\workspace1\\LaboratorioDiAlgoritmi\\src\\Image\\images23.jpg").getImage());
	 private JButton conferma=new JButton("CONFERMA");
	
	    final JTextField user_tf = new JTextField();
	    final JPasswordField cPass = new JPasswordField();
	    final JTextField email = new JTextField();
	    final JTextField nome1 = new JTextField();
	    final JTextField cognome1 = new JTextField();
	    final JTextField matricola1= new JTextField();
	    final JPasswordField pass_tf = new JPasswordField();
	    JDialog frame;
	    private String cP;
	    private String em;
	    private String nm;
	    private String cgm;
	    private String mat;
	    private String usr;
	    private String psw ;
	 private   JLabel l=new  JLabel("ERRORE : i campi * non sono stati compilati corretamente");
	    public Registrazione() {
	 
	        frame = new JDialog();
	        frame.setModal(true);
	        frame.setLocation(350, 50);
	        frame.setTitle("REGISTRAZIONE");
			 frame.setSize(600,600);
			 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			
	       
	 
	        // pannello
	        panel.setSize(new Dimension(600,600));
	        panel.setMaximumSize(new Dimension(600,600));
	        panel.setMinimumSize(new Dimension(600,600));
	        panel.setPreferredSize(new Dimension(600,600));
	        panel.setLayout(null);
	 
	        // JLabel
	        JLabel nome = new JLabel("Nome");
	        JLabel cognome = new JLabel("Cognome");
	        JLabel e_mail=new JLabel("E-mail");
	        JLabel matricola=new JLabel("Matricola");
	        JLabel user=new JLabel("Username");
	        JLabel password=new JLabel("Password");
	        JLabel cPassword=new JLabel("Conferma Password");

			l.setBounds(50,400,500, 30);
			l.setForeground(new java.awt.Color(255,0,0));//errore
	        //inserimento delle stringhe nel textField
	      usr=  user_tf.getText();
	       psw=pass_tf.getText();
	        cP=cPass.getText();
	        em=email.getText();
	        nm=nome1.getText();
	        cgm=cognome1.getText();
	        mat=matricola1.getText();
	        
	        
	        // conferma button
	        conferma.setBounds(450,510, 80, 20);
	        conferma.setBorder(null);
	        conferma.addActionListener(this);
	        conferma.setFont(new Font("Impact",Font.TRUETYPE_FONT,16));
	        conferma.setForeground(new java.awt.Color(0,0,0));
	        //user,password label
	        e_mail.setBounds(30,150, 130, 50);
	        nome.setBounds(30, 90, 100, 10);
	        cognome.setBounds(30, 120, 100, 30);
	        matricola.setBounds(30, 180, 100, 70);
	        user.setBounds(30, 210, 100, 90);
	        password.setBounds(30, 240, 100, 110);
	        cPassword.setBounds(30, 270, 150, 130);
	        // campi di inserimento
	        
	        nome1.setBounds(170, 84, 140, 20);
	        cognome1.setBounds(170, 126, 140, 20);
	        email.setBounds(170,166, 140, 20);
	       matricola1.setBounds(170,207,140,20);
	        user_tf.setBounds(170,245,140,20);
	        pass_tf.setBounds(170,285,140,20);
	        cPass.setBounds(170,325,140,20);
	        // cambio il colore della scrittura
	        nome.setForeground(new java.awt.Color(0,0,0));
	        cognome.setForeground(new java.awt.Color(0,0,0));
	        user.setForeground(new java.awt.Color(0,0,0));
	        e_mail.setForeground(new java.awt.Color(0,0,0));
	        password.setForeground(new java.awt.Color(0,0,0));
	        cPassword.setForeground(new java.awt.Color(0,0,0));
	        matricola.setForeground(new java.awt.Color(0,0,0));
	        // aggiungo al panello principale
	      
	        panel.add(user);
	        panel.add(password);
	        panel.add(cPassword);
	        panel.add(e_mail);
	        panel.add(cognome);
	        panel.add(nome);
	        panel.add(matricola);
	        panel.add(user_tf);
	        panel.add(pass_tf);
	        panel.add(conferma);
	        panel.add(cPass);
	        panel.add(email);
	        panel.add(nome1);
	        panel.add(cognome1);
	        panel.add(matricola1);
	        
	        frame.add(panel);
	        frame.setResizable(false);
	        frame.pack();
	        frame.setVisible(true);
	       }
	 	

	public static void main(String[]args){
		 
		 Registrazione f=new Registrazione();
		 
	       f.setTitle("REGISTRAZIONE");
	     
	     
	 }
	 
		   
	@Override
			//distruggi frame
	public void actionPerformed(ActionEvent ae) {
		
		if(ae.getSource()==conferma){
			em=email.getText();
			
			psw=pass_tf.getText();
	        cP=cPass.getText();
	        if(usr.length()<3){
				JLabel k=new JLabel("*");
				   k.setBounds(160, 245, 30, 20);
				   k.setForeground(new java.awt.Color(255,0,0));
				   panel.add(k);
			}
			if(psw.length()<3){
				JLabel k=new JLabel("*");
				   k.setBounds(160, 285, 30, 20);
				   k.setForeground(new java.awt.Color(255,0,0));
				   panel.add(k);
			}
			if(cP.length()<3){
				JLabel k=new JLabel("*");
				   k.setBounds(160, 325, 30, 20);
				   k.setForeground(new java.awt.Color(255,0,0));
				   panel.add(k);
			}
			if(em.length()<3){
				JLabel k=new JLabel("*");
				   k.setBounds(160, 166, 30, 20);
				   k.setForeground(new java.awt.Color(255,0,0));
				   panel.add(k);
			}
			if(nm.length()<3){
				JLabel k=new JLabel("*");
				   k.setBounds(160, 86, 30, 20);
				   k.setForeground(new java.awt.Color(255,0,0));
				   panel.add(k);
			}
			if(cgm.length()<3){
				JLabel k=new JLabel("*");
				   k.setBounds(160, 126, 30, 20);
				   k.setForeground(new java.awt.Color(255,0,0));
				   panel.add(k);
			}
			if(mat.length()<3){
				JLabel k=new JLabel("*");
				   k.setBounds(160, 207, 30, 20);
				   k.setForeground(new java.awt.Color(255,0,0));
				   panel.add(k);
			}
			if(!psw.equals(cP)){
				
				//nel campo email devono essere presenti i caratteri @ e .it
				
				if(!em.contains("@") && !em.contains(".it") ){
					
					JLabel e=new JLabel("*");
					   e.setBounds(160, 166, 30, 20);
					   e.setForeground(new java.awt.Color(255,0,0));
					   panel.add(e);
					   panel.add(l);
				}//if email
				
			        System.out.println(psw+" conf pass "+cP);
			        
			        JLabel n=new  JLabel("*");
			        JLabel m=new  JLabel("*");
			        n.setBounds(160, 285, 30, 20);
			        m.setBounds(160, 325, 30, 20);
			        n.setForeground(new java.awt.Color(255,0,0));
			        m.setForeground(new java.awt.Color(255,0,0));
				
			    	panel.add(l);
			    	panel.add(m);
				    panel.add(n);
			}//if
			
		    else {
			     if(!em.contains("@") && !em.contains(".it") ){
			         JLabel e=new JLabel("*");
			         e.setBounds(160, 166, 30, 20);
			         e.setForeground(new java.awt.Color(255,0,0));
			         panel.add(e);
			         panel.add(l);
		         }//if email
		         else{
			          System.out.println(psw+" conf pass "+cP);
			          SwingUtilities.invokeLater(new Runnable() {
				            public void run() {
				             PagPers inst = new PagPers();
				           	 inst.setLocationRelativeTo(null);
					         inst.setVisible(true);
			
		                    }//run
			          });//runnable
			         frame.dispose(); 
			     
			     }//elseif
		     }//else
		    frame.repaint();
		}//conferma
	}//actionPerformed
 }
			


